package dao

import "github.com/Amovement/password-box/pkg/models"

func ListBoxsByUsername(username string) ([]*models.Box, error) {
	db := models.GetDB()
	var boxs []*models.Box
	if err := db.Where("belong_to = ?", username).Find(&boxs).Error; err != nil {
		return nil, err
	} else {
		return boxs, nil
	}
}

func CreateBox(box *models.Box) (*models.Box, error) {
	db := models.GetDB()
	if err := db.Create(&box).Error; err != nil {
		return nil, err
	} else {
		return box, nil
	}
}

func DeleteBox(box *models.Box) error {
	db := models.GetDB()
	if err := db.Delete(&box).Error; err != nil {
		return err
	} else {
		return nil
	}
}

func GetBoxById(id uint) (*models.Box, error) {
	db := models.GetDB()
	var box *models.Box
	if err := db.Where("id = ?", id).Find(&box).Error; err != nil {
		return nil, err
	} else {
		return box, nil
	}
}
